<template>
  <!-- 116新增录入画面对话框-->
  <el-dialog
    :title="title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="editForm" :model="form" :rules="rules" label-width="130px">
      <!--交易础信息-->
      <el-card class="form-card txnInfo-card">
        <div slot="header" class="label-header-msg">
          <span>交易基础信息</span>
        </div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="业务类型" prop="bizTypeCode">
              <el-select v-model="form.bizTypeCode" placeholder="业务类型">
                <el-option
                  v-for="dict in dictMap.BIZ_TYPE_116"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务种类" prop="bizCtgyCode">
              <el-select v-model="form.bizCtgyCode" placeholder="业务种类" >
                <el-option
                  v-for="dict in dictMap.BIZ_CTGY_116"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="记账标志" prop="cbflag">
              <el-select v-model="form.cbflag" clearable placeholder="记账标志" style="width: 100%">
                <el-option
                  v-for="dict in dictMap.CBFLAG"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="交易币种" prop="curcd">
              <el-input v-model="form.curcd" placeholder="人民币-CNY" :disabled="true" style="width: 195px" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="退回金额" prop="amount">
              <ht-amount-input
                v-model="form.amount"
                placeholder="退回金额"
                style="width: 195px"
                @keyup.enter.native="checkAmountLimit()"
              ></ht-amount-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="大写金额" prop="bAmount">
              <ht-amount-upper :inputValue="form.amount" placeholder="交易金额" style="width: 195px"></ht-amount-upper>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附言" prop="remarks" >
              <el-input
                type="textarea"
                v-model="form.remarks"
                placeholder="附言"
                maxlength="135"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>

      <!--发起接收机构信息-->
      <el-card class="form-card payerInfo-card">
        <div slot="header" class="label-header-msg">
          <span>发起接收机构信息</span>
        </div>
        <el-row :gutter="10">
          <el-col :span="8">
            <div class="grid-payer">
              <el-form-item label="发起参与机构" prop="indirectSender">
                <el-input
                  v-model="form.indirectSender"
                  placeholder="发起参与机构"
                  @keyup.enter.native="getBankInf('indirectSender', form.indirectSender)"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" icon="el-icon-search" plain @click="openBankBranchDialog('indirectSender')">
              行号查找
            </el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="发起参与机构名称" prop="indirectSenderName" label-width="130px">
              <el-input v-model="form.indirectSenderName" placeholder="发起参与机构名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <div class="grid-payer">
              <el-form-item label="接收参与机构" prop="indirectRecver">
                <el-input
                  v-model="form.indirectRecver"
                  placeholder="接收参与机构"
                  @keyup.enter.native="getBankInf('indirectRecver', form.indirectRecver)"
                />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="2">
            <div class="grid-payer">
              <el-button type="primary" icon="el-icon-search" plain @click="openBankBranchDialog('indirectRecver')">
                行号查找
              </el-button>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="接收参与机构名称" prop="indirectRecverName" label-width="130px">
              <el-input v-model="form.indirectRecverName" placeholder="接收参与机构名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
      <el-button v-show="ifShowDelBtn" type="danger" @click="delTran">删 除</el-button>
    </div>
    <bank-branch-dialog ref="bankBranchDialog" @confirm="confirmBankBranch"></bank-branch-dialog>
  </el-dialog>
</template>
<script>
import { addHvps118, updataHvps118, updTxn } from '@/api/cnaps/baseBiz/send/hvps/hvps.js'
import { delTxn, detail } from "@/api/cnaps/baseBiz/send/sendCommon.js";
import { getOtherCustByActno, getCustByActno } from "@/api/cnaps/common/custInfo";
import { getBankInfByBankCode } from "@/api/cnaps/common/bankInfo";
import { isLessThenHvpsMinLimitNew } from "@/api/cnaps/common/cp2SysParam";
import BankBranchDialog from "@/views/components/BankBranchDialog";
import PayeeDialog from "@/views/components/PayeeDialog";
import PayerDialog from "@/views/components/PayerDialog";
import HtAmountInput from "@/views/components/HtAmountInput";
import HtAmountUpper from "@/views/components/HtAmountUpper";
import { mapGetters } from "vuex";

export default {
  name: "EditInfo",
  components: {
    BankBranchDialog,
    HtAmountInput,
    HtAmountUpper,
  },
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      busTypeList: [],
      id: "",
      title: "",
      pkgNo: "",
      // 是否显示弹出层
      openState: false,
      ifShowDelBtn: false,
      amount: "0.00",
      // 字段可编辑控制

      // 表单参数
      form: {
        amount: "0.00",
      },
      // 表单校验
      rules: {
        bizTypeCode: [{ required: true, message: "请输入业务类型", trigger: "blur" }],
        bizCtgyCode: [{ required: true, message: "请输入业务种类", trigger: "blur" }],
        indirectSender: [{ required: true, max: 14, message: "请输入付款行号", trigger: "blur" }],
        indirectSenderName: [{ max: 60,  trigger: "blur" }],
        indirectRecver: [{ required: true, max: 35, message: "请输入付款开户行号", trigger: ['blur', 'change'] }],
        indirectRecverName: [{ max: 140, trigger: "blur" }],
        remarks: [{ max: 140, trigger: "blur" }],
        amount: [{ required: true, message: "请输入退回金额", trigger: "blur" }],

  },
    };
  },
  mounted() {},
  computed: {
    ...mapGetters(["globalBusData"]),
  },
  methods: {
    // 表单重置
    reset() {
      this.resetForm("editForm");
      this.$set(this, "form", {});
    },
    // 关闭回掉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.reset();
          done();
        })
        .catch((_) => {});
    },
    // 检查大额交易金额上限
    checkAmountLimit() {
      isLessThenHvpsMinLimitNew(this.form.bizTypeCode, this.form.bizCtgyCode, '', this.form.amount);
    },

    openBankBranchDialog(fromFlag = "") {
      this.$refs.bankBranchDialog.show(fromFlag);
    },
    confirmBankBranch(fromFlag, row) {
      if (fromFlag == "indirectRecver") {
        this.$set(this.form, "indirectRecver", row.bankCode);
        this.$set(this.form, "indirectRecverName", row.orgFullname);
      } else if (fromFlag == "indirectSender") {
        this.$set(this.form, "indirectSender", row.bankCode);
        this.$set(this.form, "indirectSenderName", row.orgFullname);
      }
    },
    //回车取行号信息
    getBankInf(searchType, bankCode) {
      getBankInfByBankCode(bankCode).then((response) => {
        if (response.code == "S" && response.result !== null) {
          switch (searchType) {
            case "indirectSender":
              this.$set(this.form, "indirectSender", response.result.bankCode);
              this.$set(this.form, "indirectSenderName", response.result.orgFullname);
              break;
            case "indirectRecver":
              this.$set(this.form, "indirectRecver", response.result.bankCode);
              this.$set(this.form, "indirectRecverName", response.result.orgFullname);
              break;
          }
        } else {
          this.msgSuccess("查无此行号");
          switch (searchType) {
            case "indirectSender":
              this.$set(this.form, "indirectSenderName", null);
              break;
            case "indirectRecver":
              this.$set(this.form, "indirectRecverName", null);
              break;
          }
        }
      });
    },

    show(id = "", pkgNo = "") {
      this.reset();
      this.pkgNo = pkgNo;
      this.id = id;
      this.title = "大额延迟结算退回申请录入";
      this.openState = true;
      this.ifShowDelBtn = false;

      // 初始化选择
      this.$set(this.form, "bizTypeCode", this.dictMap.BIZ_TYPE_116[0].value);
      this.$set(this.form, "bizCtgyCode", this.dictMap.BIZ_CTGY_116[0].value);
      this.$set(this.form, "cbflag", this.dictMap.CBFLAG[0].value);

      this.$set(this.form, "indirectSender", this.globalBusData.cnapsBrno); //付款行号初始化当前操作员所以机构
      this.$set(this.form, "indirectSenderName", this.globalBusData.cnapsBrname);
      this.$set(this.form, "curcd", "CNY");

      if (id) {
        this.title = "大额延迟结算退回申请修改";
        this.ifShowDelBtn = true;

        // 如果传了ID 这里查询一个已经存在的数据
        detail({
          coreId: this.id,
          pkgNo: this.pkgNo,
        }).then((res) => {
          this.$set(this, "form", res.result.detail90118DataModel);
        });
      }
    },

    cancel() {
      this.openState = false;
      this.reset();
      this.$emit("editComplete", false);
    },
    /** TODO提交按钮 */
    submitForm: function () {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          //如果有ID 则是修改
          if (this.id) {
            this.form.userInfo = { brno: "676", userId: "cpg001" };
            updataHvps118(this.form).then((res) => {
              this.msgSuccess("修改成功");
              this.$emit("editComplete", true);
              this.openState = false;
            });
          } else {//新增
            //this.form.userInfo = { brno: "676", userId: "cpg001" };
              addHvps118(this.form).then((res) => {
              this.msgSuccess("新增成功");

              this.$alert(`CORE_ID: ${res.result.rspId}`, "生成交易流水号", {
                confirmButtonText: "确定",
                //刷新一下查询列表 TODO
                callback: (action) => {},
              });
              this.$emit("editComplete", true);
              this.openState = false;
            });
          }
        }
      });
    },
    /** 删除 按钮 */
    delTran: function () {
      if (this.id) {
        this.$confirm("确认删除吗？", "交易删除提示", { type: "warning" }).then((_) => {
          delTxn({ id: this.id }).then((res) => {
            this.msgSuccess("删除成功");
            this.$emit("editComplete", true);
            this.openState = false;
          });
        });
      }
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
